<template>
  <div class="execution components-container">
    <basic-container>
      <!--<div class="title">-->
        <!--<div>-->
          <el-form @submit.native.prevent :inline="true">
            <el-form-item label="配送时间:">
              <el-date-picker
                  size="medium"
                v-model="deliveryTime"
                type="date"
                  :clearable=false
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="批次:">
              <el-select v-model="buySeq" placeholder="请选择" size="medium">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="输入商品名称">
              <el-input v-model="goodsName" placeholder="请输入内容" clearable></el-input>
            </el-form-item>
            <el-form-item label="选择商品分类">
              <el-cascader
                :options="goodsCatedata"
                :props="{ checkStrictly: true }"
                clearable
                @change="handleChange"></el-cascader>
            </el-form-item>
            <el-form-item label="状态:">
              <el-select v-model="status" placeholder="请选择" size="medium">
                <el-option
                  v-for="item in opt"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
                <el-badge value="2" class="item">
                    <el-button size="medium" round @click="getSummaryList" type="primary"> 查询采购单</el-button>
                </el-badge>
            </el-form-item>

            <el-form-item>
                <el-badge value="1" class="item">
                  <el-button size="medium" round @click="shortcutCollect" type="primary"> 汇总采购单</el-button>
                </el-badge>
            </el-form-item>
          </el-form>
        <!--</div>-->
      <!--</div>-->
    </basic-container>
    <basic-container>
      <div class="body">
        <!--<div>-->
          <el-row>
            <el-col :span="10">
                <div class="demo-input-suffix">
              统一上浮报价：
              <el-input-number size="medium"
                               v-model="floatingRate"
                               :precision="2"
                               :step="0.01" :max="10" style="margin-top: 5px;">
              </el-input-number>
                </div>
            </el-col>
              <el-col :span="5">
                      运营成本：
                  <div style="width:100px;margin-right:10px;">
                      <el-input v-model="operatingCost" size="medium"></el-input>
                  </div>
                      元
              </el-col>
              <el-col :span="5">
                  物流成本：<div style="width:100px;margin-right:10px;">
                  <el-input v-model="logisticsCosts" size="medium"></el-input>
              </div> 元
              </el-col>
              <el-col :span="4">
                  商品成本：{{(goodsExpenditure).toFixed(2)}} 元
              </el-col>
          </el-row>
        <!--</div>-->
        <div >
          <el-row>
            <el-col :span="4">
              总销售额：{{(totalBuyerBillAmount).toFixed(2)}} 元
            </el-col>
            <el-col :span="4">
              毛利：{{probableProfit}} 元
            </el-col>
            <el-col :span="4">
              净利润：{{netProfit}} 元
            </el-col>
            <el-col :span="4">
              毛利率：{{probableProfitPer}} %
            </el-col>
            <el-col :span="4">
              净利率：{{netProfitPer}} %
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4">
              总重：{{totalWeight}} kg
            </el-col>
            <el-col :span="4">
              总体积：{{totalVolume}} m^3
            </el-col>
          </el-row>
          <el-row style="line-height:32px;">
              <el-col :span="4">
                  <el-badge value="3" class="item">
                      <el-button size="medium" round @click="supQuote" type="primary" v-if="!isSelfPm"> 一键供应商报价</el-button>
                  </el-badge>
              </el-col>
            <el-col :span="4">
                <el-badge value="4" class="item">
                    <el-button size="medium" round @click="sunmitTable" type="primary">提交修改信息</el-button>
                </el-badge>
            </el-col>
            <el-col :span="3"  v-if="!$constant.isPds">
                <el-badge value="5" class="item">
                    <el-button size="medium" round @click="shortcutConfirm" type="primary">交易确认</el-button>
                </el-badge>
              <el-button size="medium" round @click="purchaseOrder" v-if="isSelfPm">向供应商下单</el-button>
            </el-col>
            <el-col :span="3">
              <a class="el-button el-button--medium is-round"
                 id="bb" href @click="exportExcel">导出excel
              </a>
            </el-col>
              <el-col :span="3">
                  <el-button
                          round
                          @click="exportClick">分拣单导出
                  </el-button>
              </el-col>
            <el-col :span="3">
              <el-checkbox v-model="checked" @change="isShow">是否隐藏已成交</el-checkbox>
            </el-col>
          </el-row>
        </div>
        <div>
          <el-table
            v-loading="loading"
            :data="orderList"
            border
            stripe
            max-height="500"
            style="width: 100%">
            <el-table-column type="index" width="50" fixed>
            </el-table-column>

            <el-table-column prop="summaryOrderId" label="汇总单号" fixed>
            </el-table-column>
            <el-table-column prop="goodsName" label="名称" fixed width="120px">
              <template slot-scope="scope">
                <el-popover trigger="hover" placement="top" width="150">
                  <p>商品: {{ scope.row.goodsName }}</p>
                  <div slot="reference" class="name-wrapper">
                    <div class="goods-slot">{{ scope.row.goodsName }}</div>
                  </div>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column prop="parentCategoryName" label="一级类别" width="100px">
            </el-table-column>

            <el-table-column prop="category" label="二级类别" width="80px">
            </el-table-column>

            <el-table-column prop="goodsModel" label="规格" width="100px">
            </el-table-column>

            <el-table-column prop="unit" label="单位" width="50px">
            </el-table-column>

            <el-table-column prop="weight" label="重量(kg)" width="80px" >
            </el-table-column>

            <el-table-column prop="volume" label="体积(m^3)" width="80px">
            </el-table-column>

            <el-table-column prop="needBuyNum" label="需求数量">
            </el-table-column>

            <el-table-column prop="realBuyNum" label="实际采购数量">
            </el-table-column>

            <el-table-column prop="stockNum" label="库存">
            </el-table-column>

            <el-table-column prop="lossPercent" label="损耗比" width="80px">
            </el-table-column>

            <el-table-column prop="platformPrice" label="报价(元)">

              <el-table-column label="统一报价(销售均价)" width="150px">
                <template slot-scope="scope">
                  {{scope.row.avgBuyPrice}}
                </template>
              </el-table-column>
              <el-table-column label="采购商详情" width="100px">
                <template slot-scope="scope">
                  <el-button type="text"
                             @click="showBuyers(scope.row,scope.$index)">采购商({{scope.row.buyOrderDetailList.length}})</el-button>
                </template>
              </el-table-column>

              <el-table-column prop="" label="供应商详情" width="100px">
                <template slot-scope="scope">
                  <el-button type="text"
                             @click="showSupplier(scope.row, scope.$index)">供应商({{scope.row.supplierList1.length}})</el-button>
                </template>
              </el-table-column>
            </el-table-column>

            <el-table-column prop="marketPrice" label="市场单价">
            </el-table-column>

            <el-table-column prop="avgBuyPrice" label="采购均价">
            </el-table-column>

            <el-table-column prop="avgSupplyPrice" label="供应均价">
              <template slot-scope="scope">
                {{ + scope.row.avgSupplyPrice}}
              </template>
            </el-table-column>

            <el-table-column prop="supplierAmount" label="供应商应收账款">
              <template slot-scope="scope">
                <el-button type="text">{{scope.row.supplierAmount}}</el-button>
              </template>
            </el-table-column>

            <el-table-column prop="buyerBillAmount" label="采购商应付账款">
              <template slot-scope="scope">
                <el-button type="text">{{scope.row.buyerBillAmount}}</el-button>
              </template>
            </el-table-column>

            <el-table-column prop="profits" label="毛利润">
              <template slot-scope="scope">
                {{ scope.row.profits}}
              </template>
            </el-table-column>

            <el-table-column label="状态" fixed="right">
              <template slot-scope="scope">
                <el-button type="text">{{statusOption[scope.row.status]}}</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!-- 打印的table -->
          <el-table
            id="ddd"
            v-loading="loading"
            :data="orderList"
            border
            stripe
            max-height="500"
            style="width: 100%;display: none">
            <el-table-column type="index" width="50">
            </el-table-column>

            <el-table-column prop="summaryOrderId" label="汇总单号" >
            </el-table-column>

            <el-table-column prop="parentCategoryName" label="一级类别" width="100px">
            </el-table-column>

            <el-table-column prop="category" label="二级类别" width="80px">
            </el-table-column>

            <el-table-column prop="goodsName" label="名称" width="120px">
            </el-table-column>

            <el-table-column prop="goodsModel" label="规格" width="100px">
            </el-table-column>

            <el-table-column prop="unit" label="单位" width="50px">
            </el-table-column>

              <el-table-column prop="buyOrderRemarks" label="需求明细数量">
              </el-table-column>

            <el-table-column prop="needBuyNum" label="需求数量">
            </el-table-column>

            <el-table-column prop="realBuyNum" label="实际采购数量">
            </el-table-column>

            <el-table-column prop="lossPercent" label="损耗比" width="80px">
            </el-table-column>

            <el-table-column prop="avgBuyPrice" label="报价(元)">
            </el-table-column>

            <el-table-column prop="marketPrice" label="市场单价">
            </el-table-column>

            <el-table-column prop="avgBuyPrice" label="采购均价">
            </el-table-column>

            <el-table-column prop="avgSupplyPrice" label="供应均价">
            </el-table-column>

            <el-table-column prop="supplierAmount" label="供应商应收账款">
            </el-table-column>

            <el-table-column prop="buyerBillAmount" label="采购商应付账款">
            </el-table-column>

            <el-table-column prop="profits" label="毛利润">
            </el-table-column>

            <el-table-column label="状态">
              <template slot-scope="scope">
                <el-button type="text">{{statusOption[scope.row.status]}}</el-button>
              </template>
            </el-table-column>

          </el-table>
          <div class="page-box">
            <div></div>
            <my-pagination
              @pagechange="handlePageChange"
              :current-page.sync="pageNo"
              :page-sizes="[20, 30, 40, 50, 100, 200, 1000]"
              :page-size="pageSize"
              layout="sizes, total, prev, pager, next"
              :total="total">
            </my-pagination>
          </div>
        </div>
        <my-drag v-if="visiable" v-model="buyPosition">
          <div slot="main" class="buyer-report">
            <div style="float:right" @click="visiable = false">X</div>
            <div class="report-title">
              <div>汇总单号：{{buyOrderDetail.summaryOrderId}}</div>
              <div>商品：{{buyOrderDetail.goodsName}}</div>
              <div>实际采购数量：{{buyOrderDetail.realBuyNum}}</div>
              <div>市场价格：{{buyOrderDetail.marketPrice}}</div>
              <div>需求数量：{{buyOrderDetail.needBuyNum}}</div>
              <div>实际采购数量：{{buyOrderDetail.realBuyNum}}</div>
              <div>采购均价：{{buyOrderDetail.avgBuyPrice}}</div>
              <div>供应均价：{{buyOrderDetail.avgSupplyPrice}}</div>
            </div>
            <v-buyer v-model="buyOrderDetail.buyOrderDetailList" >
            </v-buyer><div style="text-align: right; margin: 20px 10px 0 0">
            <el-button size="mini" type="text" @click="visiable = false">取消</el-button>
            <el-button type="primary" size="mini" @click="closeBuyers">确定</el-button>
          </div>
          </div>
        </my-drag>
        <my-drag v-if="supplierVisiable" v-model="supPosition">

          <div slot="main" class="supplier-report">
            <div style="float:right" @click="supplierVisiable = false">X</div>
            <div class="report-title">
              <div>汇总单号：{{supplierOrderDetail.summaryOrderId}}</div>
              <div>采购均价：{{supplierOrderDetail.avgBuyPrice}}</div>
              <div>供应均价：{{supplierOrderDetail.avgSupplyPrice}}</div>
              <div>需求数量：{{supplierOrderDetail.needBuyNum}}</div>
              <div>商品：{{supplierOrderDetail.goodsName}}</div>
              <div style="color:red">库存：{{supplierOrderDetail.stockNum}}</div>
              <div>市场价格：{{supplierOrderDetail.marketPrice}}</div>
              <div>实际采购数量：{{supplierOrderDetail.realBuyNum}}</div>
            </div>
            <v-supplier
              v-model="supplierOrderDetail.supplierList1"
              :original="supplierOrderDetail"
              :order-status="supplierOrderDetail.status">
            </v-supplier>
            <div style="text-align: right; margin: 20px 10px 0 0">
              <el-button size="mini" type="text" @click="supplierVisiable = false">取消</el-button>
              <el-button type="primary" size="mini" @click="closeSupplier">确定</el-button>
            </div>
          </div>
        </my-drag>
      </div>
    </basic-container>
      <!-- 分拣单导出 -->
      <el-dialog title="分拣单导出" :visible.sync="dialogPrint" width="90%">
          <div id="excelExportDiv" >
              <el-row>
                  <a class="el-button el-button--small primary is-round"
                     id="excelBtn" href @click="exportExcelSort">导出excel
                  </a>
              </el-row>
              <div id="tableExcel">
                  <el-row>
                      <el-col>配送日期{{deliveryTime}}</el-col>
                  </el-row>
                  <el-table
                          :data="excelData"
                          border>
                      <el-table-column
                              v-for="(item,index) in excelTitle"
                              :key="index"
                              :prop="item.prop"
                              :label="item.label">
                      </el-table-column>
                  </el-table>
              </div>
          </div>
      </el-dialog>
  </div>
</template>

<script>
import excel from "../../../util/pds/exportExcel.js";
import vBuyer from "./buyer.vue";
import vSupplier from "./supplier.vue";
import * as math from 'mathjs';
import request from "../request.js";
import aRequest from '../../goods/request.js'
import listFun from "./listConstructor";
import supplierVue from './supplier.vue';
import compute from './mixin.js'
import {getStore, setStore} from "@/util/store";
export default {
    name: 'backToTop-demo',
    data() {
        return {
            request: new request(),
            aRequest:new aRequest(),
            deliveryTime: this.$utils.getDiffDate(1),
            buySeq: '0',
            options: this.$dic.buySeqOptions,
            opt: this.$dic.buyerOrderCheck,
            status: "",
            pageSize: 1000,
            pageNo: 1,
            total: 0,
            loading: false,
            statusOption: this.$dic.buyerOrderStatusOption,
            orderList: [],
            totalWeight: 0,
            totalVolume: 0,
            totalNeed: 0,
            netProfit: 0,//近利
            floatingRate: 0,
            probableProfit: 0,//毛利
            goodsExpenditure: 0,//货品支出--应向供应商付款
            operatingCost: 0,//运营成本
            logisticsCosts: 0,//物流成本
            totalBuyerBillAmount: 0,//总销售额
            supplierPopList: [],
            buyerPopList: [],
            buyOrderDetail: {},//采购商在拖拽框中的处理对象
            supplierOrderDetail:{},//供应商在拖拽框中的处理对象，这是对整个一条数据的处理
            visiable: false,
            supplierVisiable: false,//供应商报价弹窗控制
            buyPosition:{
                x: 600,
                y: 300
            },
            supPosition:{
                x:600,
                y:300
            },
            waitingList: [],
            isSelfPm: getStore({name: "isSelfPm"}),
            // excel导出
            dialogPrint: false,
            excelTitle: this.initTitle(),
            excelData: [],
            goodsCatedata:[],
            categoryId:'',
            goodsName:'',
          checked:false
        }
    },
    mixins: [compute],
    components:{
        vBuyer, vSupplier
    },
    created(){
        this.getSummaryList()
        this.getSupplierList()
        this.fetchCategoryList()
    },
    mounted(){
    },
    methods:{
        fetchCategoryList() {
          this.aRequest.fetchCategoryList({}).then(res => {
            if (res.code == '200') {
              let data = this.$utils.listToTree(res.ext)
              data.forEach(function (item) {
                item.categoryType = parseInt(item.categoryType);
                item.label = item.name;
                item.value = item.id;
                if(item.goodsStatus == 0 && item.isMaletable == 1){
                  item.flag = true;
                }
                if(item.goodsStatus == 1 && item.isMaletable == 0){
                  item.flag = false;
                }
                item.children.forEach((child)=>{
                  child.categoryType = parseInt(child.categoryType);
                  if(child.goodsStatus == 0 && child.isMaletable == 1){
                    child.flag = true;
                  }
                  if(child.goodsStatus == 1 && child.isMaletable == 0){
                    child.flag = false;
                  }
                  child.label = child.name;
                  child.value = child.id;
                })
              })
              this.goodsCatedata = data
            }else if (res.code == '983') {
              this.goodsCatedata = [];
            } else {
              //this.$message.error('请求失败')
            }
          })
        },
        handleChange(value) {
          if(value[1]){
            this.categoryId = value[1]
          }else{
            this.categoryId = value[0]
          }
        },
        //提交表格
        sunmitTable(){
            this.$confirm('本次提交仅提交当前页面的采购和报价，您确认提交？').then(() => {
                this.saveSupplierList()
                this.editBuyerList()
            })
        },
        //是否隐藏已成交
        isShow(val){
          let tmp = [];
          let t = 0;
          if(val == true){
            for(var i in this.orderList){
              if(this.orderList[i].status != 3){
                tmp[t] = this.orderList[i];
                t++
              }
            }
            this.orderList = tmp;
          }else{
            this.getSummaryList()
          }
        },
        //打开采购商报价
        showBuyers(item, index){
            this.buyOrderDetail = item;
            this.visiable = true;
        },
        //关闭采购商报价
        closeBuyers(){
            this.editOneBuyer();
            this.visiable = false;
        },
        //打开供应商报价
        showSupplier(item, index){
            this.supplierOrderDetail = item;
            this.supplierVisiable = true;
        },
        //关闭供应商报价
        closeSupplier(){
            this.saveOneSupplierList();
            this.supplierVisiable = false;
        },
        handlePageChange(e){
            this.pageSize = e.pageSize;
            this.pageNo = e.pageNo;
            this.getSummaryList()
        },
        exportExcel(){
            var str = "";
            this.opt.forEach(ele => {
                if(ele.value == this.status){
                    str = ele.label
                }
            })
            excel.getExcel("ddd","",this.deliveryTime
            + this.options[this.buySeq].label
            + str + "供应单.xls","bb")
        },
        // 一键供应商报价
        supQuote(){
            this.orderList.forEach( ele => {
                let arr1 = []; // 库存供应商
                let arr2 = []; // 商家供应商
                this.waitingList.forEach(ele => {
                    if(ele.supplierType === "1"){
                        arr1[0] = {...ele};
                    }else{
                        arr2[0] = {...ele};
                    }
                })
                console.log("<>" ,ele.stockNum, ele.realBuyNum)
                if(ele.stockNum <= 0){
                    let arr3 = arr2;
                    if(arr2.length === 0){
                        arr3 = arr1;
                    }
                    ele.supplierList1 = listFun.supplierOnce( arr3, ele, ele.realBuyNum);
                }else if(ele.stockNum >= ele.realBuyNum || arr2.length === 0){
                    // 库存满足时 或 没有其他供应商
                    ele.supplierList1 = listFun.supplierOnce( arr1, ele, ele.realBuyNum);
                }else if(ele.stockNum < ele.realBuyNum){
                    let arr4 = listFun.supplierOnce( arr2, ele, ele.realBuyNum - ele.stockNum);
                    let arr5 = listFun.supplierOnce( arr1, ele, ele.stockNum);
                    ele.supplierList1 = [arr4[0],arr5[0]];
                }
            })
        },
        /**网络请求层 */
        //获取采购单汇总
        getSummaryList(){
            this.loading = true;
            this.request.getsummaryOrderList({
                deliveryDate: this.deliveryTime,
                status: this.status,
                buySeq:this.buySeq,
                pageSize: this.pageSize,
                pageNo: this.pageNo,
                categoryId:this.categoryId,
                goodsName:this.goodsName
            }).then(res => {
                if(res.code === 200){
                    this.orderList = listFun.listConstruct(res.list);
                    this.buyOrderDetail = this.orderList[0];
                    this.supplierOrderDetail = this.orderList[0];
                    this.total = res.totalRows;
                }else{
                    this.orderList = [];
                    this.$message.warning(res.msg)
                }
                this.loading = false;
            }).catch(err => {
                this.loading = false;
                this.$message.warning(err)
            })
        },
        //提交报价单修改
        saveSupplierList(){
            this.loading = true;
            this.request.saveSupplierList({
                offerOrderList:listFun.offerOrderModelCon(this.orderList)
            }).then(res => {
                if(res.code == 200){
                    this.$message.success("操作成功！")
                    this.getSummaryList();
                }else{
                    this.$message.warning(res.msg);
                }
                this.loading = false;
            }).catch(err => {
                this.loading = false;
                this.$message.warning("网络错误！")
            })
        },
        //提交总的采购单报价修改
        editBuyerList(){
            this.loading = true;
            this.request.editBuyerList( {detailList:listFun.buyOrderModelCon(this.orderList)}).then(res => {
                if(res.code == 200){
                    this.$message.success("操作成功！");
                    // this.getSummaryList();
                }else{
                    this.$message.warning(res.msg);
                }
                this.loading = false;
            }).catch(err => {
                this.loading = false;
                this.$message.warning("网络错误！")
            })
        },
        //提交单个采购报价单修改
        editOneBuyer(){
            this.loading = true;
            this.request.editBuyerList(
                    {detailList:listFun.buyOrderModelCon([this.buyOrderDetail])}
                ).then(res => {
                if(res.code == 200){
                    this.$message.success("操作成功！")
                }else{
                    this.$message.error(res.msg);
                    this.getSummaryList();
                }
                this.loading = false;
            }).catch(err => {
                this.loading = false;
                this.$message.error("网络错误！")
                this.getSummaryList();
            })
        },
        //提交单个供应商报价单修改
        saveOneSupplierList(){
            this.loading = true;
            this.request.saveSupplierList(
                    {offerOrderList: listFun.offerOrderModelCon([this.supplierOrderDetail])}
                ).then(res => {
                if(res.code == 200){
                    this.$message.success("操作成功！");
                    this.supplierOrderDetail.supplierList1.forEach((ele, index) => {
                        ele.offerId = res.ext[index]
                    })

                }else{
                    this.$message.error(res.msg);
                }
                this.loading = false;
            }).catch(err => {
            this.loading = false;
                this.$message.error("网络错误！")
                this.getSummaryList();
            })
        },
        //采购单汇总  --- 君磊农产品 --- 多商家版
        shortcutCollect(){
            this.$confirm('您将要汇总采购单,是否继续?').then(() => {
                this.loading = true;
                this.request.shortcutCollect({
                    deliveryTime: this.deliveryTime,
                    buySeq: this.buySeq
                }).then(res => {
                    if(res.code === 200){
                        this.$message.success( "汇总成功!")
                        this.getSummaryList();
                    }else{
                        this.$message.warning( res.msg)
                    }
                this.loading = false;
                }).catch(err => {
                this.loading = false;
                    this.$message.error("网络错误！")
                    this.getSummaryList();
                })
            });
        },
        //交易确认  --- 君磊农产品 --- 多商家版
        shortcutConfirm(){
            this.$confirm('您确认已经完成了全部页面的商品报价?').then(() => {
                this.loading = true;
                this.request.shortcutConfirm({
                    deliveryTime: this.deliveryTime,
                    buySeq: this.buySeq
                }).then(res => {
                    if(res.code === 200){
                        this.$message.success( "交易确认成功！")
                        this.getSummaryList();
                    }else{
                        this.$message.warning( res.msg)
                    }
                this.loading = false;
                })
            });
        },
        purchaseOrder(){
            this.$confirm('您确认向供应商下单?').then(() => {
                this.loading = true;
                this.request.purchaseOrder({
                    deliveryDate: this.deliveryTime,
                    buySeq: this.buySeq
                }).then(res => {
                    if(res.code == 200){
                        this.$message.success( "下单成功！")
                    }else{
                        this.$message.warning( res.msg)
                    }
                    this.loading = false;
                })
            });
        },
        // 获取供应商列表
        getSupplierList(){
            this.request.getSupplierList().then(res => {
                if(res.code === 200){
                    this.waitingList = res.ext;
                    this.waitingList.forEach(ele => {
                        ele.supplyNum = 9999;
                        ele.minSaleNum = 0;
                    })
                }
            })
        },
        // 订单明细转分拣数据
        transData(){
            // 订单明细用于分拣
            let sortList = [];
            this.orderList.forEach(ele =>{
                let obj = {
                    goodsName:ele.goodsName,
                    goodsModel:ele.goodsModel,
                    unit:ele.unit,
                    goodsId:ele.goodsId
                };
                // 设置总需求数量
                let total = {
                    buyerId: 'total',
                    buyerName: '总数量',
                    buyNum: ele.needBuyNum,
                };
                Object.assign(total, obj);
                sortList.push(total);

                ele.buyOrderDetailList.forEach(detail =>{
                    let detailObj = {
                        buyerId:detail.buyerId,
                        buyerName:detail.buyerName,
                        buyNum:detail.goodsNum,
                    };
                    Object.assign(detailObj, obj);
                    sortList.push(detailObj);
                });
            });
            return sortList;
        },
        // 导出excel数据 分拣单
        exportClick(){
            // 数据处理
            if(this.orderList.length === 0){
                this.$message.warning("无可分拣商品");
                return;
            }
            let sortingList = this.transData();

            let excelTitle = this.initTitle();
            let excelData = [];
            // 表头填入商家  excelTitle  label   prop对应buyerId
            let titleMap = new Map();
            // 每行为单品  excelData goodsModelId 对应一行数据 buyerId 对商品数量
            let dataMap = new Map();
            sortingList.forEach(ele =>{
                titleMap.set(ele.buyerId, ele.buyerName);
                if(dataMap.has(ele.goodsId)){
                    // buyerId 重复时(多个订单)
                    let num = dataMap.get(ele.goodsId)[ele.buyerId];
                    num = (num) ? num : 0;
                    dataMap.get(ele.goodsId)[ele.buyerId] = num + ele.buyNum;
                }else{
                    let obj = {};
                    obj[ele.buyerId] = ele.buyNum;
                    obj['goodsName'] = ele.goodsName;
                    obj['goodsModel'] = ele.goodsModel;
                    obj['unit'] = ele.unit;
                    dataMap.set(ele.goodsId, obj);
                }
            });
            titleMap.forEach(function (value, key, map) {
                excelTitle.push({
                    prop: key,
                    label: value,
                });
                if (key !== 'total') {
                    excelTitle.push({
                        prop: key + 's',
                        label: '分拣量',
                    });
                }
            });
            dataMap.forEach(function (value, key, map) {
                excelData.push(value);
            });
            console.table("excelTitle", excelTitle);
            console.table("excelData", excelData);
            this.excelTitle = excelTitle;
            this.excelData = excelData;
            this.dialogPrint=true;
        },
        exportExcelSort(){
            let str = this.deliveryTime;
            excel.getExcel("tableExcel","","分拣单"+str+".xls","excelBtn");
        },
        initTitle(){
            return [{
                label: "商品",
                prop: "goodsName"
            },{
                label: "规格",
                prop: "goodsModel"
            },{
                label: "单位",
                prop: "unit"
            }];
        }
    }
}
</script>

<style scoped lang="scss">
.placeholder-container div {
    margin: 10px;
}
.title{
    display: flex;
    background-color: white;
    margin-bottom: 20px;
    > div{
        margin-top: 21px;
        margin-left: 10px;
    }
}
.el-col{
    display: flex;
    margin-top: 10px;
}
.body{
    background-color: white;
    width: 100%;
    padding: 0px;
    > div{
        padding: 10px;
    }
    .goods-slot{
        width: 100%;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        cursor: pointer;
    }
}
.report-title{
    display: flex;
    width: 650px;
    flex-wrap: wrap;
    div{
        width:25%;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        font-size: 12px;
        line-height: 23px;
        color:#707276;
        font-weight: 600;
    }
}
.el-row{
    font-size: 14px;
    font-weight: 600;
    color: #808286;
}
.page-box{
    background-color: white;
    display: flex;
    justify-content: space-between;
}
  .summarizeBtn,.referBtn{
    padding-top: 20px;
  }
  .buyer-report,.supplier-report{
    padding: 20px;
    .report-title{
      margin-bottom: 10px;
      div{
        line-height: 28px;
      }
    }
  }
</style>
